import { getLoginUserRightAPI } from '@/api/user'
import { routes, resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    // 权限标识
    permissions: [], // 考虑到该数据会被多个功能页面使用，所以存入vuex
    // 路由规则，仅用于渲染左侧菜单，不负责路由跳转功能
    menuList: []
  },
  mutations: {
    // 存储permissions
    setPermissions (state, val) {
      state.permissions = val
    },
    // 存储menuList
    setMenuList (state, asyncRoutes) {
      state.menuList = [...routes, ...asyncRoutes] // routes是静态路由，asyncRoutes是筛选后的动态路由
    },
    // 清空路由
    resetMenu (state) {
      // 重置左侧菜单
      state.menuList = []
      // 重置路由系统
      resetRouter()
    }
  },
  actions: {
    // 获取登录角色权限
    async getLoginUserRight (store) {
      const res = await getLoginUserRightAPI()
      console.log('获取登录角色权限', res)
      store.commit('setPermissions', res.data.permissions)
      return res.data.permissions
    }
  }
}
